<template>
  <div class="chart-container">
    <el-row :gutter="20">
      <el-col>
        <el-pagination
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          :current-page.sync="queryParams.pageNum"
          :page-size.sync="queryParams.pageSize"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </el-col>
    </el-row>
    <el-divider />
    <el-row :gutter="20">
      <div class="widget-center-draggable-wrapper" :model="cataId">
        <el-col v-for="(item) in dataApiList" :key="item.id" :gutter="20" class="box-card-col">
          <el-card :body-style="{ padding: '8px'}" style="max-height: 240px" :gutter="20" class="box-card-item">
            <el-descriptions :title="item.resourceName" :column="2" border>
              <el-descriptions-item>
                <template slot="label">
                  <i class="el-icon-office-building"></i>
                  来源部门
                </template>
                {{item.createDept}}
              </el-descriptions-item>
              <el-descriptions-item>
                <template slot="label">
                  <i class="el-icon-mobile-phone"></i>
                  调用方式
                </template>
                HTTP-API
              </el-descriptions-item>
              <el-descriptions-item>
                <template slot="label">
                  <i class="el-icon-date"></i>
                  发布时间
                </template>
                {{item.createDate}}
              </el-descriptions-item>
              <el-descriptions-item>
                <template slot="label">
                  <i class="el-icon-date"></i>
                  更新日期
                </template>
                {{item.updateDate}}
              </el-descriptions-item>
              <el-descriptions-item>
                <template slot="label">
                  <i class="el-icon-mobile-phone"></i>
                  描述
                </template>
                {{item.resourceComment}}
              </el-descriptions-item>
            </el-descriptions>
            <div >
              <div class="bottom clearfix">
                <el-button-group style=" float: right; margin-right: 0px">
                  <el-button  size="medium"  icon="el-icon-download" @click="handleApply(item)" style="  float: right; margin-right: 0px">
                    申请
                  </el-button>
                  <el-button  size="medium" icon="el-icon-view" @click="handleDetail(item)" style=" float: right; margin-right: 5px">
                    <span>查看详情</span>
                  </el-button>
                </el-button-group>
              </div>
            </div>
          </el-card>
        </el-col>
      </div>
    </el-row>
  </div>
</template>

<script>
import { getCataResource } from '@/api/market/cata'
import {addUserResource, applySubmit} from "@/api/market/userresource";

export default {
  props: {
    cataId: {}
  },
  data() {
    return {
      tableHeight: document.body.offsetHeight - 310 + 'px',
      // 展示切换
      showOptions: {
        data: {},
        showList: true,
        showApply: false,
        showTableDetail: false,
        showApiDetail: false
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 表格头
      tableColumns: [
        { prop: 'username', label: '用户名称', show: true },
        { prop: 'nickname', label: '用户昵称', show: true },
        { prop: 'email', label: '电子邮箱', show: true },
        { prop: 'phone', label: '手机号码', show: true },
        {
          prop: 'cata',
          label: '所属部门',
          show: true,
          formatter: this.cataFormatter
        },
        {
          prop: 'posts',
          label: '所属岗位',
          show: true,
          formatter: this.postFormatter
        },
        {
          prop: 'roles',
          label: '所属角色',
          show: true,
          formatter: this.roleFormatter
        },
        {
          prop: 'status',
          label: '状态',
          show: true,
          formatter: this.statusFormatter
        },
        { prop: 'createTime', label: '创建时间', show: true }
      ],
      // 默认选择中表格头
      checkedTableColumns: [],
      tableSize: 'medium',
      // 数据集表格数据
      dataApiList: [],
      // 总数据条数
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cataId: '',
        resourceType: 'api'
      },
      // 高级搜索 展开/关闭
      advanced: false,
      // 部门列表
      cataOptions: [],
      defaultProps: {
        children: 'children',
        label: 'cataName'
      },
      // 状态数据字典
      statusOptions: []
    }
  },
  created() {
    this.getDicts('sys_common_status').then(response => {
      if (response.success) {
        this.statusOptions = response.data
      }
    })
    this.getList()
  },
  mounted() {
    this.initCols()
  },
  methods: {
    /** 查询数据Api列表 */
    getList() {
      this.loading = true
      getCataResource(this.queryParams).then(response => {
        this.loading = false
        if (response.success) {
          const { data } = response
          this.dataApiList = data.data
          this.total = data.total
        }
      })
    },
    /** 查询数据Api列表by cataId */
    getListByConf(cataId) {
      this.loading = true
      this.queryParams.cataId = cataId;
      getCataResource(this.queryParams).then(response => {
        this.loading = false
        if (response.success) {
          const { data } = response
          this.dataApiList = data.data
          this.total = data.total
        }
      })
    },
    initCols() {
      this.checkedTableColumns = this.tableColumns.map(col => col.prop)
    },
    handleCommand(command) {
      this.tableSize = command
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        cataId: '',
        resourceType: 'api'
      }
      this.handleQuery()
    },
    /** 刷新列表 */
    handleRefresh() {
      this.getList()
    },
    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.resourceId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.showOptions.data = {}
      this.showOptions.showList = false
      this.showOptions.showAdd = true
      this.showOptions.showEdit = false
      this.showOptions.showDetail = false
      this.$emit('showCard', this.showOptions)
    },
    /** 修改按钮操作 */
    handleApply(row) {
      this.$confirm('您正在申请该资源, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const userResource = {
          resourceId: row.resourceId,
          resourceType: 'api',
          status: 0
        }
        addUserResource(userResource).then(response => {
          if (response.success) {
            applySubmit(response.data.id).then(response2 => {
              this.loading = false
              if (response2.success) {
                this.$message({
                  type: 'success',
                  message: '提交申请成功，请耐心等待审批。您可在个人中心查看申请进度'
                });
              }
            })
          }
        })
      }).catch(() => {

      });

    },
    /** 详情按钮操作 */
    handleDetail(row) {
      this.showOptions.data.id = row.resourceId || this.ids[0]
      this.showOptions.showList = false
      this.showOptions.showApply = false
      this.showOptions.showTableDetail = false
      this.showOptions.showApiDetail = true
      this.$emit('showCard', this.showOptions)
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.queryParams.pageNum = 1
      this.queryParams.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.queryParams.pageNum = val
      this.getList()
    },
    /** 节点单击事件 */
    handleNodeClick(data) {
      this.queryParams.cataId = data.id
      this.getList()
    },
    toggleAdvanced() {
      this.advanced = !this.advanced
    }
  }
}
</script>

<style lang="scss" scoped>
.right-toolbar {
  float: right;
}
.el-card ::v-deep .el-card__body {
  height: calc(100vh - 170px);
}
.tree-wrapper {
  overflow-y: auto;
}

.bottom {
  margin-top: 8px;
  line-height: 10px;
}

.button {
  padding: 0;
  float: right;
}

.image {
  width: 100%;
  display: block;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both
}
</style>
